import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:study_online/entity/TestItemEn.dart';
import 'package:study_online/pages/courseStudyPage/views/TestItem.dart';

import '../CourseStudyModel.dart';

class TestTab extends StatefulWidget {
  TestTab({Key key}) : super(key: key);

  @override
  _TestTabState createState() {
    return _TestTabState();
  }
}

class _TestTabState extends State<TestTab> {
  TestItemList _testItemList = new TestItemList([]);
  int couId = 1;

  void _loadData() async {
    Response response = await getTestListById(couId);
    print("###" + response.toString());
    var responseData = json.decode(response.toString());
    setState(() {
      _testItemList = TestItemList.formJson(responseData);
    });
  }

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );

    if (_testItemList == null) {
      return Container(
        width: 0,
        height: 0,
      );
    } else {
      return Container(
        margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(38),
          ScreenUtil().setHeight(0),
          ScreenUtil().setWidth(38),
          ScreenUtil().setHeight(0),
        ),
        child: ListView.builder(
          itemCount: _testItemList.data.length,
          scrollDirection: Axis.vertical,
          itemBuilder: (BuildContext context, int index) {
            return new TestItem(
              testItemEn: _testItemList.data[index],
            );
          },
        ),
      );
    }
  }
}
